package com.example.limit.controller;

import com.example.limit.rule.PurchaseRuleEngine;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/rule-engine")
public class RuleEngineController {
    
    private final PurchaseRuleEngine ruleEngine;
    
    public RuleEngineController(PurchaseRuleEngine ruleEngine) {
        this.ruleEngine = ruleEngine;
    }
    
    @PostMapping("/check")
    public PurchaseRuleEngine.RuleEngineResult checkPurchaseLimit(
            @RequestParam Long userId,
            @RequestParam String productId,
            @RequestParam String categoryId,
            @RequestParam int quantity,
            @RequestParam(required = false) Boolean isNewUser,
            @RequestParam(required = false) String userLevel,
            @RequestParam(required = false) String activityId) {
        
        Map<String, Object> context = new HashMap<>();
        if (isNewUser != null) context.put("isNewUser", isNewUser);
        if (userLevel != null) context.put("userLevel", userLevel);
        if (activityId != null) context.put("activityId", activityId);
        
        return ruleEngine.checkPurchaseLimit(userId, productId, categoryId, quantity, context);
    }
    
    @PostMapping("/complex-check")
    public Map<String, Object> complexCheckPurchaseLimit(
            @RequestBody PurchaseCheckRequest request) {
        
        PurchaseRuleEngine.RuleEngineResult result = ruleEngine.checkPurchaseLimit(
            request.getUserId(), request.getProductId(), request.getCategoryId(), 
            request.getQuantity(), request.getContext());
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", result.isPassed());
        response.put("message", result.getMessage());
        response.put("ruleDetails", result.getRuleDetails());
        response.put("timestamp", java.time.LocalDateTime.now());
        
        return response;
    }
    
    /**
     * 购买检查请求体
     */
    public static class PurchaseCheckRequest {
        private Long userId;
        private String productId;
        private String categoryId;
        private Integer quantity;
        private Map<String, Object> context;
        
        // getters and setters
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        public String getProductId() { return productId; }
        public void setProductId(String productId) { this.productId = productId; }
        public String getCategoryId() { return categoryId; }
        public void setCategoryId(String categoryId) { this.categoryId = categoryId; }
        public Integer getQuantity() { return quantity; }
        public void setQuantity(Integer quantity) { this.quantity = quantity; }
        public Map<String, Object> getContext() { return context; }
        public void setContext(Map<String, Object> context) { this.context = context; }
    }
}